// 可乐零售机

#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;

// 货架
class Dispenser {
private:
    string beverage;    // 饮料
    float cost;
    int total;          // 库存

public:
    Dispenser() { cost = 0; total = 0; }

    void SetStock(string beverage, float cost, int total) {
        this->beverage = beverage;
        this->cost = cost;
        this->total = total;
    }

    float HowMuchDoICost() { return cost; }

    void WhatAmI() { cout << setw(15) << beverage; }

    void LetEmHaveIt();

    int CheckStock() { return total; }
};

// 投币器
class MoneyCtr {
private:
    float input_amount;

public:
    MoneyCtr() { input_amount = 0; }

    void GetMoney();

    float HowMuchDoWeHave() { return input_amount; }

    void Clear() { input_amount = 0; }

    void ReturnMoney(float change);
};

// 贩卖机
class VendingMachine {
private:
    MoneyCtr bank;
    Dispenser D[5];

public:
    VendingMachine();
    void ShowChoices();
    void InsertMoney() { bank.GetMoney(); }
    bool DispenseItem(int index);
    void ReturnAllMoneyAndExit();
};

int main() {
    VendingMachine cola;

    bool go_again = true;
    while (go_again) {
        // 给我现金
        bool gimme_cash = true;
        while (gimme_cash) {
            cola.InsertMoney();
            cout << "继续投币吗? yes 或者 no: ";

            char answer[5];
            cin.getline(answer, 5);
            if (strcmp(answer, "no") == 0) {
                gimme_cash = false;
            }
        }
        cola.ShowChoices();

        char selectbuf[5];
        cin.getline(selectbuf, 5);
        int selection = atoi(selectbuf);

        if (selection == 5) {
            cola.ReturnAllMoneyAndExit();
            go_again = false;
        }
        else if (cola.DispenseItem(selection)) {
            go_again = false;
        }
        else {
            cout << "选其他饮料吗? yes 或 no: ";
            char answer[5];
            cin.getline(answer, 5);
            if (strcmp(answer, "yes")) {
                cola.ReturnAllMoneyAndExit();
                go_again = false;
            }
        }
    }
}

void Dispenser::LetEmHaveIt() {
    cout << "你选择了 " << beverage << ", 好好享受!\n";
    total--;
}

void MoneyCtr::GetMoney() {
    float coin;
    cout << "请输入硬币面值, 25美分 = 25, 10美分 = 10, 等等: ";
    cin >> coin;
    cin.ignore(25, '\n');
    input_amount += coin / 100.0f;
    cout << "投币总数 $" << input_amount << "\n";
}

void MoneyCtr::ReturnMoney(float change) {
    cout << "退回给你的钱是 $" << change << "\n";
    Clear();
}

VendingMachine::VendingMachine() {
    D[0].SetStock("Coke", 1.0f, 25);
    D[1].SetStock("Pepsi", 1.0f, 25);
    D[2].SetStock("BrandX Cola", 0.8f, 25);
    D[3].SetStock("Blue Sky Cola", 1.25f, 25);
    D[4].SetStock("Jolt", 2.5f, 0);
}

void VendingMachine::ShowChoices() {
    cout.setf(ios::fixed);
    cout.precision(2);

    cout << "你输入了 $" << bank.HowMuchDoWeHave() << "\n";
    for (int i = 0; i < 5; i++) {
        cout << i << " ";
        D[i].WhatAmI();
        cout << " ($" << D[i].HowMuchDoICost() << ")\n";
    }
    cout << "5 退款并退出程序\n";
    cout << "输入饮料号码(价格): ";
}

bool VendingMachine::DispenseItem(int index) {
    int total = D[index].CheckStock();
    if (total <= 0) {
        cout << "已售完\n";
        return false;
    }

    if (bank.HowMuchDoWeHave() < D[index].HowMuchDoICost()) {
        cout << "钱不够\n";
        return false;
    }

    D[index].LetEmHaveIt();
    float change = bank.HowMuchDoWeHave() - D[index].HowMuchDoICost();
    if (change > 0) {
        bank.ReturnMoney(change);
    }
    return true;
}

void VendingMachine::ReturnAllMoneyAndExit() {
    bank.ReturnMoney(bank.HowMuchDoWeHave());
}
